layui.use(['jquery','form', 'ax','admin','layedit', 'laydate'], function(){
    var $ =layui.jquery
    var form = layui.form
    var layer = layui.layer
    var layedit = layui.layedit
    var laydate = layui.laydate
    var $ax = layui.ax;
    var admin = layui.admin;


    $(function () {
        //加载日程项信息
        var sframeId=$('#sframeId').val();
        alert(sframeId+"123");
        $.post({
            url:"/myScheduleFrame/reviseSframe1?sframeId="+sframeId,
            success:function (data) {
               var scheduleFrameItems=data.scheduleFrameItems;
                var htmladd="";
                for (var i=0;i<scheduleFrameItems.length;i++) {
                   var startTime=timestampToTime(scheduleFrameItems[i].startTime);
                    var stopTime=timestampToTime(scheduleFrameItems[i].stopTime);

                    htmladd+="        <div class=\"layui-card\" id=\"Mess\">\n" +
                        "            <div class=\"layui-card-header\">日程框架项信息</div>\n" +
                        "            <div class=\"layui-card-body\">\n" +
                        "                <div class=\"layui-form-item layui-row\">\n" +
                        "                    <input name=\"userId\" type=\"hidden\"/>\n" +
                        "                    <div class=\"layui-inline layui-col-md12\">\n" +
                        "                        <label class=\"layui-form-label\">框架项序号<span style=\"color: red;\">*</span></label>\n" +
                        "                        <div class=\"layui-input-block\">\n" +
                        "                            <input name=\"serial[]\" placeholder=\"请输入框架项序号\" value='"+scheduleFrameItems[i].serialNumber+"' type=\"text\" class=\"layui-input\" lay-verify=\"required\" required/>\n" +
                        "                        </div>\n" +
                        "                    </div>\n" +
                        "                    <div class=\"layui-inline layui-col-md12\">\n" +
                        "                        <label class=\"layui-form-label\">框架项名称<span style=\"color: red;\">*</span></label>\n" +
                        "                        <div class=\"layui-input-block\">\n" +
                        "                            <input name=\"scheduleItemName[]\" placeholder=\"请输入框架项名称\" value='"+scheduleFrameItems[i].sframeItemName+"' type=\"text\" class=\"layui-input\" lay-verify=\"required\" required/>\n" +
                        "                        </div>\n" +
                        "                    </div>\n" +
                        "                    <div class=\"layui-inline layui-col-md12\">\n" +
                        "                        <label class=\"layui-form-label\">开始时间</label>\n" +
                        "                        <div class=\"layui-input-inline\">\n" +
                        "                            <input  name=\"startTime[]\" placeholder=\"请选择框架项的开始时间\" type=\"text\" value='"+startTime+"' class=\"layui-input date-icon datatime\" autocomplete=\"off\"/>\n" +
                        "                        </div>\n" +
                        "                        <label class=\"layui-form-label\">结束时间</label>\n" +
                        "                        <div class=\"layui-input-inline\">\n" +
                        "                            <input  name=\"stopTime[]\" placeholder=\"请选择框架的结束时间\" type=\"text\" value='"+stopTime+"' class=\"layui-input date-icon datatime\" autocomplete=\"off\"/>\n" +
                        "                        </div>\n" +
                        "                    </div>\n" +
                        "                </div>\n" +
                        "            </div>\n" +
                        "        </div>"


                }
                $("#sframeItemMess").append(htmladd);
                form.render();
                showData();
                showYear();

            }
        });
    });


    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        alert(JSON.stringify(data));
        console.log(data)
        var ajax = new $ax(Feng.ctxPath + "/myScheduleFrame/changeAndSaveSframe", function (data) {
            Feng.success("框架修改成功！");
            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);
            //关掉对话框
            admin.closeThisDialog();
            parent.location.href="/myScheduleFrame/showMyScheduleFrame";

        }, function (data) {
            Feng.error("框架！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        //添加 return false 可成功跳转页面
        return false;
    });




    $(document).on('click', '#sframeItemAddBtn', function() {

        var htmladd="";
        htmladd+="        <div class=\"layui-card\" id=\"Mess\">\n" +
            "            <div class=\"layui-card-header\">日程框架项信息</div>\n" +
            "            <div class=\"layui-card-body\">\n" +
            "                <div class=\"layui-form-item layui-row\">\n" +
            "                    <input name=\"userId\" type=\"hidden\"/>\n" +
            "                    <div class=\"layui-inline layui-col-md12\">\n" +
            "                        <label class=\"layui-form-label\">框架项序号<span style=\"color: red;\">*</span></label>\n" +
            "                        <div class=\"layui-input-block\">\n" +
            "                            <input name=\"serial[]\" placeholder=\"请输入框架项序号\" type=\"text\" class=\"layui-input\" lay-verify=\"required\" required/>\n" +
            "                        </div>\n" +
            "                    </div>\n" +
            "                    <div class=\"layui-inline layui-col-md12\">\n" +
            "                        <label class=\"layui-form-label\">框架项名称<span style=\"color: red;\">*</span></label>\n" +
            "                        <div class=\"layui-input-block\">\n" +
            "                            <input name=\"scheduleItemName[]\" placeholder=\"请输入框架项名称\" type=\"text\" class=\"layui-input\" lay-verify=\"required\" required/>\n" +
            "                        </div>\n" +
            "                    </div>\n" +
            "                    <div class=\"layui-inline layui-col-md12\">\n" +
            "                        <label class=\"layui-form-label\">开始时间</label>\n" +
            "                        <div class=\"layui-input-inline\">\n" +
            "                            <input  name=\"startTime[]\" placeholder=\"请选择框架项的开始时间\" type=\"text\" class=\"layui-input date-icon datatime\" autocomplete=\"off\"/>\n" +
            "                        </div>\n" +
            "                        <label class=\"layui-form-label\">结束时间</label>\n" +
            "                        <div class=\"layui-input-inline\">\n" +
            "                            <input  name=\"stopTime[]\" placeholder=\"请选择框架的结束时间\" type=\"text\" class=\"layui-input date-icon datatime\" autocomplete=\"off\"/>\n" +
            "                        </div>\n" +
            "                    </div>\n" +
            "                </div>\n" +
            "            </div>\n" +
            "        </div>"

        $("#sframeItemMess").append(htmladd);
        form.render();
        showData();
        showYear();
    });

    //删除最后一个框架项框架
    $(document).on('click', '#sframeItemDelBtn', function() {
        $("#Mess:last").remove();
    });



    function showData(){
        lay('.datatime').each(function () {
            //时间范围
            laydate.render({
                elem: this,
                trigger: 'click'
                ,type: 'time'

            });

        });

    }

    function showYear(){
        lay('.dataYear').each(function () {
            //选择年份
            laydate.render({
                elem: this,
            });

        });

    }
    showData();
    showYear();

/*    $(document).on('click', '.datatime', function() {
        //时间范围
        laydate.render({
            elem: '.datatime',
            trigger: 'focus'
            ,type: 'time'
            ,range: true
        });
    });*/


    function timestampToTime(timestamp) {
        var date = new Date(timestamp);
        var y = date.getFullYear();
        var m = date.getMonth()+1;
        m = m<10?'0'+m:m;
        var d = date.getDate();
        d = d<10?("0"+d):d;
        var h = date.getHours();
        h = h<10?("0"+h):h;
        var M = date.getMinutes();
        M = M<10?("0"+M):M;
        var s=date.getSeconds();
        s=s<10?("0"+s):s;
        var str = h+":"+M+":"+s;
        return str;
    }




});
